package nl.sogyo.objectvisualisatie.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import nl.sogyo.objectvisualisatie.core.Model;


public class MenuBar implements Observer, ActionListener {
	
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem menuItemRun, menuItemClear, menuAutopos, menuAutopos2, menuInterrupt, menuResume, menuNextStep;
	private Model model;
	
	public MenuBar()
	{
		//Create the menu bar.
		menuBar = new JMenuBar();
		
		menu = new JMenu("Options");
		menu.setMnemonic(KeyEvent.VK_E);
		
		menuBar.add(menu);
		
		//run button
		menuItemRun = new JMenuItem("Run", KeyEvent.VK_T);
		menuItemRun.addActionListener(this);
		menu.add(menuItemRun);

		//clear button
		menuItemClear = new JMenuItem("Clear screen", KeyEvent.VK_T);
		menuItemClear.addActionListener(this);
		menuItemClear.setEnabled(false);
		menu.add(menuItemClear);
		
		//autoposition false button
		menuAutopos = new JMenuItem("autoposition false     ", KeyEvent.VK_T);
		menuAutopos.addActionListener(this);
		menu.add(menuAutopos);
		
		//autoposition true button
		menuAutopos2 = new JMenuItem("autoposition true", KeyEvent.VK_T);
		menuAutopos2.addActionListener(this);
		menuAutopos2.setEnabled(false);
		menu.add(menuAutopos2);
		
		//interrupt button
		menuInterrupt = new JMenuItem("Interrupt", KeyEvent.VK_T);
		menuInterrupt.addActionListener(this);
		menu.add(menuInterrupt);
		
		//autoposition true button
		menuResume = new JMenuItem("Resume", KeyEvent.VK_T);
		menuResume.addActionListener(this);
		menuResume.setEnabled(false);
		menu.add(menuResume);
		
		//next step
		menuNextStep = new JMenuItem("Next step", KeyEvent.VK_T);
		menuNextStep.addActionListener(this);
		menu.add(menuNextStep);
	}
	
	public JMenuBar getMenu(){
		return menuBar;
    }

	@Override
	public void update() {
		// TODO Auto-generated method stub
	}

	public void initalise(Model model) {
		this.model = model;
		model.attach(this);
	}

	public void actionPerformed(ActionEvent e) {
        JMenuItem source = (JMenuItem)(e.getSource()); // detect which item was used
        String text = source.getText();
        if (text.equals("Run")){
        	InputPane pane = new InputPane();
        	int result = JOptionPane.showConfirmDialog(null, 
        		   		pane.getComponent(), // place the component into the JOptionPane 
        		        "Select source files", 
        		        JOptionPane.OK_CANCEL_OPTION);
        	if(result == JOptionPane.OK_OPTION)
        	{
        		model.setClasspath("-classpath "+pane.getClasspath());
        		model.setMainClass(pane.geMainclass());
        		model.setSourcepath(pane.geSourcepath());
        		menuItemRun.setEnabled(false);
        		model.startTrace();
        		menuItemClear.setEnabled(true);
        	}
        }
        else if (text.equals("Clear screen")){
            menuItemClear.setEnabled(false);
            model.removeAll();
            menuItemRun.setEnabled(true);
        }
        else if (text.equals("autoposition false     ")){
        	menuAutopos.setEnabled(false);
        	model.setAutoposition(false);
            menuAutopos2.setEnabled(true);
        }
        else if (text.equals("autoposition true")){
        	menuAutopos2.setEnabled(false);
        	model.setAutoposition(true);
            menuAutopos.setEnabled(true);
        }
        else if (text.equals("Interrupt")){
        	menuInterrupt.setEnabled(false);
        	model.setThreadContinue(false);
        	menuResume.setEnabled(true);
        }
        else if (text.equals("Resume")){
        	menuResume.setEnabled(false);
        	model.setThreadContinue(true);
            menuInterrupt.setEnabled(true);
        }
        else if (text.equals("Next step")){
        	model.setNext(true);
        }
	}            	
}            	
